<?php
namespace Home\Controller;
use Think\Controller;
// +----------------------------------------------------------------------
// | Fanwe 方维p2p借贷系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class LoginController extends  BaseController{
	//首页
    public function index()
    {

        if($this->is_weixin()){
            //$_SESSION['user']['openid'] = 'oo94b02-QGTBhEaiwP5iBsx28a3w';
            $openid = \es_session::get("wx_user_openid") ;
            $wx_user_openid = \es_session::get('wx_user_openid');
            $user_name = \es_session::get('user_name');
            if(empty($wx_user_openid)){
                header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4a6d0b9fa17894ce&redirect_uri=http%3a%2f%2fwap.yzlicai.com%2findex.php%3fm%3dHome%26c%3dUserwxregister%26a%3dindex&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect');
                exit;
            }else{
                $wx_rs = M('weixin_user')->where('openid="'.$wx_user_openid.'"')->find();
                if($wx_rs){
                    wap_user_login( $openid);
                    $user_info = \es_session::get("user_info");

                    if($user_info['id'] > 0 ){
                        header('Location:'.SITE_DOMAIN.'/index.php?m=home&c=uccenter&a=index');
                    }else{
                        //去绑定
                        header('Location:'.SITE_DOMAIN.'/index.php?m=home&c=userwxregister&a=index&wx_openid='.$wx_user_openid.'&user_name='.$user_name);
                    }
                }else{
                    header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4a6d0b9fa17894ce&redirect_uri=http%3a%2f%2fwap.yzlicai.com%2findex.php%3fm%3dHome%26c%3dUserwxregister%26a%3dindex&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect');
                    exit;
                }

            }
        }
        //$root['act'] = "login";
        $root['program_title'] = "登录";
        $root['act'] = CONTROLLER_NAME;
        $this->assign('data',$root);
        $this->display();
    }
    public function is_weixin() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }

    public function weixin_version() {
        preg_match('/.*?(MicroMessenger\/([0-9.]+))\s*/', $_SERVER['HTTP_USER_AGENT'], $matches);
        echo '你的微信版本号为:'.$matches[2];
    }
}
?>